<template>
	<view>
		<view @click="on_bind_wx()" class="p3 flex a-c j-b" style="height: 120rpx;">
			<view class="flex a-c">
				<text class="f30 ml2 ">微信</text>
			</view>
			<view class="flex a-c ">
				<text class="icon_color f28 ">{{is_wx_bind?'已绑定':'立即绑定'}}</text>
				<text class="bicon icon_color f28 ">&#xe6a3;</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				is_wx_bind: '',
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			on_bind_wx() {
				var that = this;
				// #ifdef MP-WEIXIN
				//微信小程序授权登录
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						that.ajax_wx_bind(loginRes.code, 'mp');
					}
				});
				// #endif
				// #ifdef APP
				this.app_wx_bind();
				// #endif
			},
			//app微信授权登录
			app_wx_bind() {
				var that = this
				uni.getProvider({
					service: 'oauth',
					success: function(res) {

						if (~res.provider.indexOf('weixin')) {
							uni.login({
								"provider": "weixin",
								"onlyAuthorize": true,
								success: function(res) {
									var code = res.code;
									that.wx_code = res.code;
									let requestParams = {
										app_wx_code: code,
									};
									that.ajax_wx_bind(code, 'app');
								},
								fail: function(res) {
									uni.showToast({
										title: res.errMsg,
										icon: 'none'
									})

								}
							})
						}
					}
				});
			},
			ajax_wx_bind(code, platform) {
				uni.showLoading({
					title: '正在绑定中...',
					mask: true
				})
				let requestParams = {
					code: code,
					bind_platform: platform
				};
				this.Http.post('/banmi.php/Bind/WxBind',requestParams).then(ret => {

					uni.showToast({
						title: ret.msg,
						icon: 'none'
					})
					if (ret.status == 200) {
						this.is_wx_bind = true
					}
				})
			},
			init() {
				let requestParams = {};
				this.Http.post('/banmi.php/Bind/index',requestParams).then(ret => {


					if (ret.status == 200) {
						this.wx_bind = ret.data.wx_bind;
						if (this.wx_bind) {
							// #ifdef MP-WEIXIN
							this.is_wx_bind = this.wx_bind.openid
							// #endif
							// #ifdef APP
							this.is_wx_bind = this.wx_bind.app_openid
							// #endif
						}



					}
				})
			},
		}
	}
</script>

<style>

</style>